<?php
    namespace app\api\model;
    use think\Model;
    use think\Db;
    
//  model 类
class MessageModel extends Model
{
    // 返回未读消息条数 
    /* 
    * list 为true返回 未读消息数组，false 返回消息数量
    */
    public static function getUnreadMessageNum($user_id ='',$deadline='',$list=false){ 
        if(!$user_id){
            return false;
        }
        if(!$deadline){
            $deadline = time();
        }

        $push_time=time()- $deadline;
        $where['is_push']=2;
        $where['push_time']=['>',$push_time];

        $msg_ids=Db('push_message')->field('msg_id')->where($where)->select();
        $msg_ids=array_column($msg_ids,'msg_id');

        $count=0;
        $unreadMessageList = [];
        foreach ($msg_ids as $key => $value) {
            $res=Db('read_message')->where(['msg_id'=>$value,'user_id'=>$user_id])->find();

            if($res){
                // 有已读记录
                
            }else{
                // 没有已读记录
                $unreadMessageList[] =Db('push_message')->where(['msg_id'=>$value])->field('title,content')->find();
                $count++;
            }
            
        }
        return $list? $unreadMessageList:$count;


    }
    
}

